package splug.cjk;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import splug.cjk.packet.FriendPacket;

public class FriendRequestRead extends Thread {

	private OutputStream os;
	private String id;
	
	
	public FriendRequestRead(OutputStream os, String id) {
		super();
		this.os = os;
		this.id = id;
	}


	@Override
	public void run() {
		super.run();
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/carpool?useUnicode=true&characterEncoding=euckr","root","apmsetup");
			Statement stmt = conn.createStatement();

			String query1 = "select f from friend where t = '" + id + "' and accept = 0";

			stmt.execute("SET CHARACTER SET euckr");
			stmt.execute("set names euckr");

			ResultSet rs1 = stmt.executeQuery(query1);
			
			ArrayList<String> content = new ArrayList<String>();
			

			while(rs1.next()){
				String str = rs1.getString("f");
				content.add(str);
			}
			
			FriendPacket packet = new FriendPacket(content, "", "");
			ObjectOutputStream output = new ObjectOutputStream(os);
			output.writeObject(packet);
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
